/*
 * Copyright (C) 2013 gujicheng
 *
 * Licensed under the GPL License Version 2.0;
 * you may not use this file except in compliance with the License.
 *
 * If you have any question, please contact me.
 *
 *************************************************************************
 **                   Author information                                **
 *************************************************************************
 ** Email: gujicheng197@126.com                                         **
 ** QQ   : 29600731                                                     **
 ** Weibo: http://weibo.com/gujicheng197                                **
 *************************************************************************
 */
package com.libra.sinvoice

import java.util.HashMap

object Common {
    // 开始标志位
    val START_TOKEN = 0
    // 结束标志位
    val STOP_TOKEN = 6
    // 默认的编码本
    val CODE_BOOK = "12345"

    val INDEX = intArrayOf(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 6, -1, -1, -1, -1, 5, -1, -1, -1, 4, -1, -1, 3, -1, -1, 2, -1, -1, 1, -1, -1, 0)

    val CHARS = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_~`!@#$%^&*()+=,./<>?;'\\:\"|[]{}- "

    val CODE_MAP: MutableMap<Char, String> = HashMap()
    val CHAR_MAP: MutableMap<String, Char> = HashMap()

    val FREQUENCY_STEP = 100
    // 默认缓冲区大小
    val DEFAULT_BUFFER_SIZE = 4096
    // 默认缓冲区数量
    val DEFAULT_BUFFER_COUNT = 3
    // 默认采样率
    val DEFAULT_SAMPLE_RATE = 44100

    fun init() {
        if (CODE_MAP.isNotEmpty() && CHAR_MAP.isNotEmpty()) {
            return
        }
        val ca = CODE_BOOK.toCharArray()

        var i = 0
        var j = 1
        var k = 2
        var m = 3
        CHARS.forEach { x ->
            run {
                val str = String(charArrayOf(ca[i], ca[j], ca[k], ca[m]))
                CODE_MAP[x] = str
                CHAR_MAP[str] = x
                do {
                    if (++m >= ca.size) {
                        k++
                        m = 0
                    }
                    if (k >= ca.size) {
                        j++
                        k = 0
                    }
                    if (j >= ca.size) {
                        i++
                        j = 0
                    }
                    if (i >= ca.size) {
                        return@forEach
                    }
                } while (i == j || i == k || i == m || j == k || j == m || k == m)
            }
        }
    }
}
